package com.fanxuankai.boot.upload.dao.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fanxuankai.boot.upload.Status;
import com.fanxuankai.boot.upload.dao.JobDao;
import com.fanxuankai.boot.upload.dao.ReadItemDao;
import com.fanxuankai.boot.upload.dao.WriteItemDao;
import com.fanxuankai.boot.upload.domain.Job;
import com.fanxuankai.boot.upload.mapper.JobMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author fanxuankai
 */
@Service
public class JobDaoImpl extends ServiceImpl<JobMapper, Job> implements JobDao {
    @Resource
    private ReadItemDao readItemDao;
    @Resource
    private WriteItemDao writeItemDao;

    @Override
    public Float calcProgress(long id) {
        Job job = getById(id);
        if (job == null) {
            return null;
        }
        Status status = Status.valueOf(job.getStatus());
        if (status == Status.UPLOADED) {
            return 0.0f;
        }
        if (status == Status.COMPLETED) {
            return 1.0f;
        }
        int readItemCount = readItemDao.count(id);
        if (readItemCount == 0) {
            return 0.0f;
        }
        return (float) writeItemDao.count(id) / readItemCount;
    }
}
